#include<iostream>
#include<cmath>
#define int long long
using namespace std;
int n,m,k,po[18],_a[100001],c[100001],d[18][65537],T,x[4],a[100001],ans,f[150001];
string ddd;
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	cin>>n>>m;
	po[0]=1;
	for(k=1;;k++){
        po[k]=po[k-1]*2;
        if(po[k]>=n)break;
	}
	for(int i=1;i<=n;i++)cin>>_a[i];
	for(int i=1;i<=m;i++)cin>>c[i];
	for(int i=1;i<=k;i++){
		cin>>ddd;
        for(int j=1;j<=po[k-i];j++)d[i][j]=ddd[j-1]-'0';
	}
    cin>>T;
    while(T--){
        for(int i=0;i<4;i++)cin>>x[i];
        for(int i=1;i<=n;i++)a[i]=_a[i]^x[i%4];
        ans=0;
        for(int i=1,kk,ss;i<=m;i++){
            kk=(c[i]==1?0:(int)(log2(c[i]-1))+1);
            ss=po[kk]*(po[kk]+1)/2;
            for(int j=1;j<=po[kk];j++)f[j]=j;
            for(int j=1;j<=kk;j++){
                for(int l=1;l<=po[kk-j];l++){
                    if(d[j][l]){
                        if(f[l*2]>c[i])f[l]=f[l*2];
                        else{
                            if(a[f[l*2]]>=j)ss-=f[l*2-1],f[l]=f[l*2];
                            else ss-=f[l*2],f[l]=f[l*2-1];
                        }
                    }else{
                        if(f[l*2-1]>c[i])f[l]=f[l*2-1];
                        else{
                            if(a[f[l*2-1]]>=j)ss-=f[l*2],f[l]=f[l*2-1];
                            else ss-=f[l*2-1],f[l]=f[l*2];
                        }
                    }
                }
            }
            ans=ans^(ss*i);
        }
        cout<<ans<<'\n';
    }
	return 0;
}
//dead program
